<template>
  <common-info-dialog
    :title="title"
    :visible.sync="open"
    @cancel="cancel"
    width="900px"
  >
    <common-list>
      <common-table
        v-loading="loading"
        :data="list"
        slot="content"
        style="height: 418px"
      >
        <el-table-column
          v-for="item in tableList"
          :label="item.lable"
          :prop="item.prop"
          :key="item.prop"
          show-overflow-tooltip
          :width="item.width ? item.width : ''"
        />
      </common-table>
      <pagination
        slot="pagination"
        v-show="
          total > 0 &&
          this.type !== 'ipNum' &&
          this.type !== 'sourceIpStrList' &&
          this.type !== 'targetIpStrList'
        "
        :total="total"
        :page.sync="query.pageNum"
        :limit.sync="query.pageSize"
        @pagination="getList"
      />
    </common-list>
  </common-info-dialog>
</template>
<script>
import BaseList from "@/mixins/BaseList";
import { searchVisitDetail } from "@/api/apisecurity/portrait/index";
export default {
  mixins: [BaseList],
  props: { account: { type: Object } },
  data() {
    return {
      title: "",
      buttonLoading: false,
      open: false,
      query: {
        pageNum: 1,
        pageSize: 10,
        account: this.account.account,
        beginTime: this.account.beginTime,
        endTime: this.account.endTime,
      },
      list: [],
      loading: false,
      total: 1,
      type: "",
      tableList: [],
    };
  },
  watch: {},
  computed: {},
  methods: {
    show(type, row) {
      this.query.account = this.account.account;
      this.query.beginTime = this.account.beginTime;
      this.query.endTime = this.account.endTime;
      this.type = type;
      this.open = true;
      if (type === "errorVisit") {
        this.title = "异常访问次数";
        this.query.searchType = "ABNORMAL";
        this.getList();
      }
      if (type === "totalVisit") {
        this.title = "访问次数";
        this.getList();
      }

      if (type === "interfaceNum") {
        this.title = "访问接口数";
        this.query.searchType = "INTERFACE";
        this.getList();
      }
      if (type === "sensitiveNum") {
        this.title = "去重敏感数据";
        this.query.searchType = "SENSITIVEDATA";
        this.getList();
      }
      if (type === "visitTime" && row) {
        this.title = "访问时间分布";
        this.query.searchType = "TIME";
        this.query.createTime = row;
        this.getList();
      }
      if (type === "sensitive" && row) {
        this.title = "敏感数据分布";
        this.query.searchType = "SENSITIVE";
        this.query.sensitiveData = row;
        this.getList();
      }
      if (type === "visitIP" && row) {
        this.title = "访问源IP分布";
        this.query.searchType = "IP";
        this.query.sourceIp = row;
        this.getList();
      }
      if (type === "sourceIpStrList") {
        this.title = "访问源IP";
        this.list = row?.sourceIps?.reduce((r, c) => {
          r.push({ sourceIp: c });
          return r;
        }, []);
        this.tableList = [{ lable: "访问源IP", prop: "sourceIp" }];
      }
      if (type === "targetIpStrList") {
        this.title = "访问目标IP";
        this.tableList = [{ lable: "访问目标IP", prop: "targetIp" }];
        this.list = row?.targetIps?.reduce((r, c) => {
          r.push({ targetIp: c });
          return r;
        }, []);
      }
      if (type === "ipNum") {
        this.title = "使用IP数";
        this.list = row?.sourceIps?.reduce((r, c) => {
          r.push({ sourceIp: c });
          return r;
        }, []);
        this.tableList = [{ lable: "IP", prop: "sourceIp" }];
      }
    },
    getList() {
      this.loading = true;
      searchVisitDetail(this.query)
        .then((res) => {
          if (res && res.code === 200) {
            if (
              this.type === "errorVisit" ||
              this.type === "visitTime" ||
              this.type === "visitIP" ||
              this.type === "totalVisit"
            ) {
              this.list = res?.rows;
              this.tableList = [
                { lable: "源IP", prop: "sourceIpStr" },
                { lable: "目标IP", prop: "targetIpStr" },
                { lable: "访问时间", prop: "visitTime" },
                { lable: "访问状态", prop: "statusCode", width: "80" },
                { lable: "URL", prop: "apiUrl" },
              ];
            }
            if (this.type === "sensitive") {
              this.list = res?.rows;
              this.tableList = [
                { lable: "源IP", prop: "sourceIpStr" },
                { lable: "目标IP", prop: "targetIpStr" },
                { lable: "敏感字段", prop: "interfaceSensitiveRule" },
                { lable: "访问时间", prop: "visitTime" },
                { lable: "访问状态", prop: "statusCode", width: "80" },
                { lable: "URL", prop: "apiUrl" },
              ];
            }
            if (this.type === "interfaceNum") {
              this.tableList = [
                { lable: "URL", prop: "apiUrl" },
                { lable: "访问次数", prop: "totalVisit", width: "120" },
              ];
              this.list = res?.rows;
            }
            if (this.type === "sensitiveNum") {
              this.tableList = [{ lable: "敏感数据", prop: "sensitiveDetail" }];
              this.list = res?.rows;
            }
            this.total = res?.total;
          } else {
            this.total = 0;
            this.list = [];
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },

    cancel() {
      this.open = false;
      this.query = {
        pageNum: 1,
        pageSize: 10,
        account: this.account.account,
        beginTime: this.account.beginTime,
        endTime: this.account.endTime,
      };
      this.tableList = [];
    },
  },
  created() {},
  mounted() {},
};
</script>
